package top.malegod.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

/**
 * Create by IntelliJ IDEA
 *
 * @author: AbelEthan
 * @date-Time: 2018/4/17 15:43
 * @description:  返回数据工具类
 */
@JsonInclude(Include.NON_NULL)
public class ServerResponse<T> {

  private static final int SUCCESS = 0;

  private static final int ERROR = 1;

  private static final int NOTLOG = 10;

  private Integer status;

  private T data;

  private String msg;

  public ServerResponse() {

  }

  public ServerResponse(Integer status, T data) {
    this.status = status;
    this.data = data;
  }
  public Integer getStatus() {
    return status;
  }

  public void setStatus(Integer status) {
    this.status = status;
  }

  public T getData() {
    return data;
  }

  public void setData(T data) {
    this.data = data;
  }

  public String getMsg() {
    return msg;
  }

  public void setMsg(String msg) {
    this.msg = msg;
  }

  public boolean success(){
    return this.status.equals(createSuccess().status);
  }

  public ServerResponse(Integer status, T data, String msg) {
    this.status = status;
    this.data = data;
    this.msg = msg;
  }

  public static ServerResponse create(int i,Object...args){
    if (args.length == 0){
      return new ServerResponse(i,null,null);
    }
    if (args.length == 1){
      if (args[0] instanceof String){
        return new ServerResponse(i,null,(String)args[0]);
      }else {
        return new ServerResponse<>(i, args[0],null);
      }
    }
    if (args.length == 2){
      if (args[0] instanceof String){
        return new ServerResponse(i,args[1],(String) args[0]);
      }else{
        return new ServerResponse(i,args[0],(String) args[1]);
      }
    }
    return null;
  }

  public static ServerResponse createSuccess(Object... args){
    return create(ServerResponse.SUCCESS,args);
  }

  public static ServerResponse createError(Object... args){
    return create(ServerResponse.ERROR,args);
  }
}
